package edu.sias.hyzn.controller;

import edu.sias.common.core.controller.BaseController;
import edu.sias.common.core.domain.AjaxResult;
import edu.sias.common.core.page.TableDataInfo;
import edu.sias.hyzn.domain.Medicine;
import edu.sias.hyzn.domain.MedicineRecord;
import edu.sias.hyzn.service.IMedicineService;
import edu.sias.hyzn.vo.MedicineRecordPie;
import edu.sias.hyzn.vo.MedicineRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/hyzn/medicine")
public class MedicineController extends BaseController {

    private final IMedicineService medicineService;

    @Autowired
    public MedicineController(IMedicineService medicineService) {
        this.medicineService = medicineService;
    }

    @GetMapping("/list")
    public TableDataInfo list(Medicine medicine) {
        startPage();
        List<Medicine> list = medicineService.selectMedicineList(medicine);
        return getDataTable(list);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody Medicine medicine){
        log.debug("add: {}", medicine);
        return toAjax(medicineService.insertMedicine(medicine));
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody Medicine medicine){
        return toAjax(medicineService.updateMedicine(medicine));
    }

    @DeleteMapping("/delete")
    public AjaxResult delete(Long id){
        return toAjax(medicineService.deleteMedicineById(id));
    }

    @GetMapping("/record-list")
    public TableDataInfo recordList(){
        startPage();
        List<MedicineRecordVO> list = medicineService.selectMedicineRecordVOList();
        return getDataTable(list);
    }

    @GetMapping("/record-list/{id}")
    public AjaxResult recordList(@PathVariable Long id){
        if (id == null) {
            return AjaxResult.error("参数错误");
        }
        List<MedicineRecordPie> list = medicineService.selectMedicineRecordVOById(id);
        return AjaxResult.success(list);
    }

    @PostMapping("/record-add")
    public AjaxResult recordAdd(@RequestBody MedicineRecord medicineRecord){
        return toAjax(medicineService.insertMedicineRecord(medicineRecord));
    }

    @PutMapping("/record-update")
    public AjaxResult recordUpdate(@RequestBody MedicineRecord medicineRecord){
        return toAjax(medicineService.updateMedicineRecord(medicineRecord));
    }

    @DeleteMapping("/record-delete")
    public AjaxResult recordDelete(Long id){
        return toAjax(medicineService.deleteMedicineRecordById(id));
    }
}
